# -*- coding:utf-8 -*-
import json
from rest_framework.validators import UniqueValidator
from rest_framework import serializers
from account.serializers import UserDetailSerializer
from models import Article,CommentFrist,CommentSecondary,Plate,Label


class CommentSecondarySerializers(serializers.ModelSerializer):
    class Meta:
        model = CommentSecondary
        fields = "__all__"


class CommentFristSerializers(serializers.ModelSerializer):
    comments_secondary = CommentSecondarySerializers(many=True)
    class Meta:
        model = CommentFrist
        fields = "__all__"


class ArticleCreateSerializers(serializers.Serializer):
    user = serializers.HiddenField(
        default=serializers.CurrentUserDefault()
    )
    plate = serializers.PrimaryKeyRelatedField(required=True, queryset=Plate.objects.all())
    label = serializers.PrimaryKeyRelatedField(required=True, queryset=Label.objects.all())
    details = serializers.CharField(required=True)
    abstract = serializers.CharField(required=True)
    file = serializers.FileField(max_length=None, allow_empty_file=False, use_url=True)
    name = serializers.CharField(required=True)
    def create(self, validated_data):
        label = validated_data.pop('label')
        existed = Article.objects.create(**validated_data)
        existed.label.add(label)
        return existed


    def to_representation(self, obj):
        labels = []
        labelobj = obj.label.all()
        for label in labelobj:
            labels.append(
                label.name
            )
        return {
            'plate': obj.plate.name,
            'label': labels,
            'details': obj.details,
            'abstract': obj.abstract,
            'file': str(obj.file),
            'name': obj.name,
        }

    # class Meta:
    #     model = Article
    #     fields = ("details",'abstract','file','visit_number','thumb_number','name','user','plate','label','comments')



class ArticleSerializers(serializers.ModelSerializer):
    comments = CommentFristSerializers(many=True)
    plate = serializers.SerializerMethodField(read_only=True)
    label = serializers.SerializerMethodField(read_only=True)
    user = UserDetailSerializer(many=False)

    def get_plate(self,obj):
        return {
            'name': Plate.objects.filter(id=obj.plate_id)[0].name,
            'id': Plate.objects.filter(id=obj.plate_id)[0].id
        }
    def get_label(self,obj):
        labels = []
        labels_obj_list = Label.objects.all()
        for labels_obj in labels_obj_list:
            articles = labels_obj.articles_label.all()
            for article in articles:
                if article.id == obj.id:
                    labels.append(
                        {
                            'name': labels_obj.name,
                            'id': labels_obj.id
                        }
                    )
        return labels
    class Meta:
        model = Article
        fields = ("details",'abstract','file','visit_number','thumb_number','name','user','plate','label','comments')



class PlateArticleSerializers(serializers.ModelSerializer):
    articles_plate = ArticleSerializers(many=True)
    class Meta:
        model = Plate
        fields = "__all__"


class PlateSerializers(serializers.ModelSerializer):
    class Meta:
        model = Plate
        fields = ("name",)


class LabelArticleSerializers(serializers.ModelSerializer):
    articles_label = ArticleSerializers(many=True)
    class Meta:
        model = Label
        fields = "__all__"

class LabelSerializers(serializers.ModelSerializer):
    class Meta:
        model = Label
        fields = ("name",)
